<?php

/**
 * This is the model class for table "product_powerleveling".
 *
 * The followings are the available columns in table 'product_powerleveling':
 * @property integer $id
 * @property integer $pl_game
 * @property string $pl_level
 * @property string $pl_price
 * @property string $pl_time
 */
class ProductPowerleveling extends CActiveRecord {
    public $maxlevel,$minlevel;
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return ProductPowerleveling the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'product_powerleveling';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('pl_game, pl_price, pl_time, pl_level', 'required'),
            array('pl_game', 'numerical', 'integerOnly' => true),
            array('pl_level', 'length', 'max' => 4),
            array('pl_price, pl_time', 'length', 'max' => 10),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, pl_game, pl_level, pl_price, pl_time', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'pl_game' => '游戏ID',
            'pl_level' => '等级',
            'pl_price' => '价格($)',
            'pl_time' => '时间(小时)',
            'update_time' => '最后修改时间'
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('pl_game', $this->pl_game);
        $criteria->compare('pl_level', $this->pl_level);
        $criteria->order = 'pl_game ASC, pl_level ASC';
        $pageSize = !empty($this->pl_game) ? 10000 : 10;

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                    'pagination' => array(
                        'pageSize' => $pageSize,
                    ),
                ));
    }
    
    /**
     * 生成输入框
     * @author flwof
     * @param type $name
     * @param type $id
     * @param type $val 
     */
    public static function createInput($name, $id, $val) {
        echo CHtml::textField("{$name}_{$id}", $val, array('rel-id' => $id, 'rel-field' => $name, 'onchange' => "updateField(this)"));
    }
    /**
      +-----------------------------------------------------
     * 说明 获取相应等级的 代练时间，代练价格
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getPowerlevelPH($gid,$slv,$elv){
        $cdbc = new CDbCriteria();
        $cdbc->compare('pl_game', $gid);
        $cdbc->compare('pl_level', ">".$slv);
        $cdbc->compare('pl_level', "<=".$elv);
        $cdbc->select = "SUM(pl_price) as pl_price,SUM(pl_time) as pl_time";
        $data = $this->find($cdbc);
        $data->pl_price = WebPrice::model()->getFunctionPrice($data->pl_price, $gid, '', 'PL');
        return $data;
    }
    /**
      +-----------------------------------------------------
     * 说明 获取最大等级，最小等级
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getMaxMinLevel($gid){
        $cdbc = new CDbCriteria;
        $cdbc->compare('pl_game', $gid);
        $cdbc->select = "MAX(pl_level) AS maxlevel,MIN(pl_level) AS minlevel";
        return $this->find($cdbc);
    }
}